package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbee.smartcommunities.entity.Category;
import com.fbee.smartcommunities.entity.Permission;
import com.fbee.smartcommunities.service.CategoryService;
import com.fbee.smartcommunities.service.PermissionService;
import com.fbee.smartcommunities.utils.CustomizeException;
import com.fbee.smartcommunities.vo.ResultVO;
import io.minio.errors.*;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResultVO<List<Category>> list(){
        return ResultVO.success(categoryService.getlist());
    }

    @GetMapping("/getById")
    public ResultVO<Category> getById(Long id){
        return ResultVO.success(categoryService.getById(id));
    }

    @PostMapping("/del")
    public ResultVO<Boolean> delById(Long id) throws CustomizeException {
        return ResultVO.success(categoryService.delete(id),"删除成功");
    }

    @PostMapping("/add")
    public ResultVO<Boolean> add(String name, Long parentId, Integer level, Integer type, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(categoryService.addCategory(name,parentId,level,type,file),"添加成功");
    }

    @PostMapping("/update")
    public ResultVO<Boolean> update(Long id, String name,Integer type,MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(categoryService.update(id,name,type,file),"修改成功");
    }

    @GetMapping("/check")
    public ResultVO<Boolean> check(String filedName, String value, Long id){
        return ResultVO.success(categoryService.check(filedName, value, id));
    }

    @GetMapping("/child")
    public ResultVO<Boolean> hasChild(Long id){
        int size = categoryService.list(new QueryWrapper<Category>().eq("parent_id", id)).size();
        return ResultVO.success(size > 0 ? true : false);
    }

}
